package utils;

import lombok.extern.slf4j.Slf4j;
import model.NodeInfo;
import model.NodeRole;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;

/**
 * 配置加载工具类
 */
@Slf4j
public class ConfigUtils {

    private static final Properties properties = new Properties();

    static {
        try (InputStream input = ConfigUtils.class.getClassLoader().getResourceAsStream("application.properties")) {
            if (input == null) {
                log.error("找不到 application.properties 文件");
            } else {
                properties.load(input);
                log.info("成功加载配置文件 application.properties");
            }
        } catch (IOException ex) {
            log.error("加载配置文件失败", ex);
        }
    }

    public static String getProperty(String key) {
        return properties.getProperty(key);
    }

    public static String getProperty(String key, String defaultValue) {
        return properties.getProperty(key, defaultValue);
    }
    
    public static int getIntProperty(String key, int defaultValue) {
        try {
            return Integer.parseInt(properties.getProperty(key, String.valueOf(defaultValue)));
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }

    public static boolean getBooleanProperty(String key, boolean defaultValue) {
        return Boolean.parseBoolean(properties.getProperty(key, String.valueOf(defaultValue)));
    }
    
    public static NodeRole getNodeRole() {
        String role = getProperty("node.role", "SINGLE").toUpperCase();
        try {
            return NodeRole.valueOf(role);
        } catch (IllegalArgumentException e) {
            log.warn("无效的节点角色: {}, 将使用默认角色 SINGLE", role);
            return NodeRole.SINGLE;
        }
    }
    
    public static NodeInfo getMasterNode() {
        String master = getProperty("cluster.master");
        if (master == null || master.trim().isEmpty()) {
            return null;
        }
        try {
            String[] parts = master.split(":");
            return new NodeInfo(parts[0], Integer.parseInt(parts[1]));
        } catch (Exception e) {
            log.error("无效的主节点配置: {}", master, e);
            return null;
        }
    }

    public static List<NodeInfo> getSlaveNodes() {
        String slaves = getProperty("cluster.slaves");
        if (slaves == null || slaves.trim().isEmpty()) {
            return java.util.Collections.emptyList();
        }
        return Arrays.stream(slaves.split(","))
                .map(String::trim)
                .map(slave -> {
                    try {
                        String[] parts = slave.split(":");
                        return new NodeInfo(parts[0], Integer.parseInt(parts[1]));
                    } catch (Exception e) {
                        log.error("无效的从节点配置: {}", slave, e);
                        return null;
                    }
                })
                .filter(java.util.Objects::nonNull)
                .collect(Collectors.toList());
    }
} 